// ActionScript file
package {
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.text.*;
	
	public class WBSnode extends Sprite {
		
		private var project_rollover:Project_rollover_mc = new Project_rollover_mc();
		private var nodeContainer:Sprite = new Sprite();		
		private var field:TextField = new TextField();
		private var fieldTempContainer:TextField = new TextField();
		public var fieldWidth:Number = 10;
		private var padding:int = 10;
		private var childNest:Sprite = new Sprite();
		private var currentHeight:Number;
		private var startingLocation:Point = new Point();
		private var labelID:String = new String();
		private var field_wbs_id:TextField = new TextField();
		
		
		private var objLoader:Loader;
		private var il:Number;
		private var objData:Object;
		
		private var	nodeSpace:Number;
		private var nodeWsum:Number;		
		private var titleField:TextField = new TextField();
		
		private var nodeBG:Sprite = new Sprite();
		
		public var drag:Boolean = false;
		public var drop:Boolean = false;
		
		public function WBSnode(label:String = ""):void {
			
			
			addChild(nodeBG);
			
			
			//draws initial field
			field.type = TextFieldType.INPUT;
			field.autoSize = TextFieldAutoSize.CENTER;
			field.wordWrap = true;
			field.multiline = true;
			field.border = true;
			field.background = true;
			field.width = 150;
			
			field.addEventListener(Event.CHANGE,fieldChange);
			field.x = padding;
			field.y = padding;
			field.text = label;
			addChild(field);
			drawBG();
			nodeContainer.x = 50;
			nodeContainer.y = field.y + field.height + 20;
			addChild(nodeContainer);
			
			currentHeight = field.height;			
			
			
			addEventListener(Event.ENTER_FRAME, enterFrameFunc);
			addEventListener(MouseEvent.MOUSE_OVER, hover);
			addEventListener(MouseEvent.MOUSE_OUT, leave);
			nodeBG.addEventListener(MouseEvent.MOUSE_DOWN, dragNodes);
			nodeBG.addEventListener(MouseEvent.MOUSE_UP, dropNodes);
			project_rollover.project_rollover_titlebar_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragNodes);
			/* project_rollover.project_rollover_titlebar_mc.addEventListener(MouseEvent.MOUSE_UP, dropNodes);
			project_rollover.project_rollover_close_btn_mc.addEventListener(MouseEvent.MOUSE_DOWN, removeNodes); */
			mouseEnabled = true;
		}
		
		/* private function addEventListeners():void{
			node.addEventListener(MouseEvent.MOUSE_DOWN, pickup);
			node.addEventListener(MouseEvent.MOUSE_UP, place);
			node.mouseEnabled = true;		
		} */
		
		private function hover(e:MouseEvent):void {
			nodeContainer.mouseEnabled = false;			
		}
		
		private function leave(e:MouseEvent):void{
			nodeContainer.mouseEnabled = true;
			
		}
		
		private function dragNodes(e:MouseEvent):void{
			/* fieldTempContainer = field;
			nodeContainer.removeChild(field); */
									
			startingLocation.x = e.target.parent.x;
			startingLocation.y = e.target.parent.y;			
			e.target.parent.startDrag();
			e.target.parent.filters = [ new DropShadowFilter() ];
			//trace(DisplayObjectContainer(e.target));
			//setChildIndex(DisplayObjectContainer(e.target), numChildren - 1);
			/* if ((startingLocation + e.currentTarget) > 150 ){
				nodeRedraw(e.currentTarget);
			} */
			e.target.parent.drag = true;
			trace("what is it? " + e.target.parent.name);
		}
		
		public function dropNodes(e:MouseEvent):void {
			e.target.parent.stopDrag();
			e.target.parent.filters = null;
			
			var nodeTarget:Array = getObjectsUnderPoint (new Point(mouseX, mouseY));
			trace(nodeTarget);
			/* var dropTargets:Array = getObjectsUnderPoint (new Point(mouseX, mouseY));
			
			if(dropTargets [ dropTargets.length - 2 ] == _white) {
				var color:uint;
				switch(event.target) {
					case _red: color = 0xFF0000; break;
					case _green:color = 0x00ff00; break;
					case _blue:color = 0x0000ff; break;
				}
				
				_white.graphics.clear();
				_white.graphics.beginFill(color);
				_white.graphics.drawRoundRect( 20, 10, 10, 10, 2, 2 );
			}*/
			
			/* event.target.x = startingLocation.x-3;
			event.target.y = startingLocation.y-3;  */
			e.target.parent.drag = false;					
		}
		
		private function fieldChange(e:Event):void{
			/* trace("change: " + e.target.textWidth); */
			/* if(field.textWidth > 149){
				trace("change width");
				field.autoSize = TextFieldAutoSize.CENTER;
				field.width = 150;
				field.multiline = true;
				field.wordWrap = true;
			} */
			//do an if that checks to see if the height has changed. If it has broadcast event.
			field.autoSize = TextFieldAutoSize.CENTER;
			drawBG();
			/* trace("this.height: " + this.height + " : currentHeight: " + currentHeight); */
			if(field.height != currentHeight){
				currentHeight = field.height;
				nodeContainer.y = field.y + field.height + 20;
				trace("calculated value of field in loop: " + (field.y + field.height));
				dispatchEvent(new Event("nodeChange",false,false));
			}
			//drawNestBG();	
		}
		
		private function nodePlace(e:MouseEvent):void{
			var nodeTarget:Array = getObjectsUnderPoint (new Point(mouseX, mouseY));			
		}
		
		private function drawNestBG():void{
			nodeContainer.graphics.clear();
			nodeContainer.graphics.beginFill(0x000000, 0);
			nodeContainer.graphics.drawRect(0,0, this.width, this.height);
			trace("drawNestBG " + this);
		}
		
		private function drawBG():void{
			addChild(project_rollover)
			
			nodeBG.graphics.clear();
			nodeBG.graphics.beginFill(0xff00d8);
			nodeBG.graphics.drawRoundRect(0,0,field.width+(padding*2),field.height+(padding*2),5,5);
			trace("field: " + field.name + ", width: " + field.width + ", height: " + field.height); 
		}
		
		public function get returnTextWidth():Number{
			return fieldWidth;
		}
		
		public function set inputTextWidth(n:Number):void
		{
			fieldWidth = n;
		}
		
		public function addNode(node:*):void{
			//do crazy logic to find the next node position based on how many nodes are in the tree already
			node.x = 50;
			nodeContainer.addChild(node)
			layoutNodes();
			node.addEventListener("nodeChange",handleNodeChange);
			node.addEventListener("detatchNode", handleDetatchNode);
		}
		
		/* public function nodeRedraw(node:*):void{
			node.x = 50;
			nodeContainer.addChild(node);
			layoutNodes();
			node.addEventListener("nodeChange", handleNodeChange);
		} */
		
		private function handleNodeChange(e:Event):void{
			//loop over children and re layout the children based on new heights
			layoutNodes()
		}
		
		private function layoutNodes():void{
			var temp:Number = 0;
			for(var i:int=0; i< nodeContainer.numChildren;i++){
				nodeContainer.getChildAt(i).y = temp;
				temp += nodeContainer.getChildAt(i).height+10;
				
				
			}
		}
		
		private function enterFrameFunc(e:Event):void {
			if(drag == true){
				
				
				var pt1:Point = new Point(this.x, this.y);
				var pt2:Point = new Point(startingLocation.x, startingLocation.y);
				var distance:Number = Point.distance(pt1, pt2);
				trace("d: " + distance + " this: " + this);
				
				if (drop == true) {			
					if (distance > 300){
						dispatchEvent(new Event("detatchNode",false,false));										
					}else{
						e.target.parent.x = e.target.parent.startingLocation.x;
						e.target.parent.y = e.target.parent.startingLocation.y;
					}
				}
				//only do this when a distance is reached
				dispatchEvent(new Event("detatchNode",false,false));
			}
			
		}
		
		private function handleDetatchNode(e:Event):*{
			//stop the event from bubbling up more
			//remove the node specified by e.target
			//Attach it to the mouse on the stage.
			//call layoutNodes
			//this.parent
			if(this == e.target){
				trace("true");
				
			}
			trace("detatchNode" );
		}
		
		
		private function addWbsId():TextField{
			field_wbs_id.type = TextFieldType.DYNAMIC;
			field_wbs_id.autoSize = TextFieldAutoSize.CENTER;
			field_wbs_id.wordWrap = false;
			field_wbs_id.multiline = false;
			field_wbs_id.border = false;
			field_wbs_id.background = false;
			field_wbs_id.width = 20;
			addChild(field_wbs_id);
		}
		
	}
}
